package com.api.shopapi.mapper;

import com.api.shopapi.entity.Goods;
import com.api.shopapi.entity.dto.GoodsDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GoodsMapper {

    @Insert("insert into goods " +
            "(" +
            "GoodsCode," +
            "BusinessCode," +
            "Name," +
            "NameMore," +
            "Stock," +
            "SaleCount," +
            "Head1," +
            "Head2," +
            "Head3," +
            "Head4," +
            "Head5," +
            "Details," +
            "GoodsAttributeCodes," +
            "GoodsType," +
            "Status," +
            "CreateTime," +
            "IsRecommend," +
            "IsDelete," +
            "IsAPP," +
            "IsPad," +
            "IsBargainPrice," +
            "AuditStatus," +
            "AuditMessage," +
            "GovernmentCode," +
            "GoodsClassGovernmentCode," +
            "GoodsClassGovernmentCodePath," +
            "GoodsClassGovernmentName," +
            "GoodsClassBusinessCode," +
            "GoodsClassBusinessCodePath," +
            "GoodsClassBusinessName," +
            "Price," +
            "PriceMin " +
            ")value(" +
            "#{goods.GoodsCode} ," +
            "#{goods.BusinessCode} ," +
            "#{goods.Name} ," +
            "#{goods.NameMore} ," +
            "#{goods.Stock} ," +
            "#{goods.SaleCount}," +
            "#{goods.Head1}," +
            "#{goods.Head2}," +
            "#{goods.Head3}," +
            "#{goods.Head4}," +
            "#{goods.Head5} ," +
            "#{goods.Details} ," +
            "#{goods.GoodsAttributeCodes} ," +
            "#{goods.GoodsType} ," +
            "#{goods.Status} ," +
            "#{goods.CreateTime}," +
            "#{goods.IsRecommend}," +
            "#{goods.IsDelete}," +
            "#{goods.IsAPP}," +
            "#{goods.IsPad}," +
            "#{goods.IsBargainPrice}," +
            "#{goods.AuditStatus} ," +
            "#{goods.AuditMessage}," +
            "#{goods.GovernmentCode}," +
            "#{goods.GoodsClassGovernmentCode}," +
            "#{goods.GoodsClassGovernmentCodePath}," +
            "#{goods.GoodsClassGovernmentName}," +
            "#{goods.GoodsClassBusinessCode}," +
            "#{goods.GoodsClassBusinessCodePath}," +
            "#{goods.GoodsClassBusinessName}, " +
            "#{goods.Price}," +
            "#{goods.PriceMin}  " +
            ")")
    int insert(@Param("goods") Goods goods);

    @Update("update goods set " +
            "Name=#{goods.Name} ," +
            "NameMore=#{goods.NameMore} ," +
            "Stock=#{goods.Stock} ," +
            "SaleCount=#{goods.SaleCount} ," +
            "Head1=#{goods.Head1}," +
            "Head2=#{goods.Head2}," +
            "Head3=#{goods.Head3}," +
            "Head4=#{goods.Head4}," +
            "Head5=#{goods.Head5}," +
            "Details=#{goods.Details} ," +
            "GoodsAttributeCodes=#{goods.GoodsAttributeCodes},  " +
            "GoodsType=#{goods.GoodsType} ," +
            "Status=#{goods.Status} ," +
            "IsRecommend=#{goods.IsRecommend} ," +
            "IsDelete=#{goods.IsDelete} ," +
            "IsAPP=#{goods.IsAPP} ," +
            "IsPad=#{goods.IsPad} ," +
            "IsBargainPrice=#{goods.IsBargainPrice}," +
            "AuditStatus=#{goods.AuditStatus}," +
            "AuditMessage=#{goods.AuditMessage}," +
            "GovernmentCode=#{goods.GovernmentCode},  " +
            "GoodsClassGovernmentCode=#{goods.GoodsClassGovernmentCode},  " +
            "GoodsClassGovernmentCodePath=#{goods.GoodsClassGovernmentCodePath},  " +
            "GoodsClassGovernmentName=#{goods.GoodsClassGovernmentName}, " +
            "GoodsClassBusinessCode=#{goods.GoodsClassBusinessCode},  " +
            "GoodsClassBusinessCodePath=#{goods.GoodsClassBusinessCodePath},  " +
            "GoodsClassBusinessName=#{goods.GoodsClassBusinessName}," +
            "Price=#{goods.Price} ," +
            "PriceMin=#{goods.PriceMin}  " +
            " where GoodsCode=#{goods.GoodsCode} ")
    int update(@Param("goods") Goods goods);




    @Select("select  * from  goods where GovernmentCode=#{governmentCode} and IsRecommend=1")
    List<GoodsDTO> getByGovernmentCode(@Param("governmentCode") String governmentCode);

    @Select("<script>" +"select  * from  goods where GovernmentCode=#{governmentCode} "+
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='goodsClassGovernmentCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
            " and GoodsClassGovernmentCode=#{goodsClassGovernmentCode} " +
            "</if>" +
            "</script> ")
    List<GoodsDTO> getByGovernmentCode1(@Param("governmentCode") String governmentCode,
                                        @Param("name") String name,
                                        @Param("goodsClassGovernmentCode") String goodsClassGovernmentCode);


    @Select("<script>" +
            "select GoodsCode from goods" +
            " where GovernmentCode=#{governmentCode}"+
            "<if test ='businessCode !=\"\" '>" +
            " and businessCode = #{businessCode} " +
            "</if>" +
            " order by createTime desc"+
            "</script> ")
    List<Goods> ListByGovernmentCodeBusinessCode(@Param("governmentCode") String governmentCode,
                                                 @Param("businessCode") String businessCode);

    @Select("<script>" +
            "select GoodsCode from goods" +
            " where GovernmentCode=#{governmentCode} and isPad = 1 "+
            "<if test ='businessCode !=\"\" '>" +
            " and businessCode = #{businessCode} " +
            "</if>" +
            " order by createTime desc"+
            "</script> ")
    List<Goods> ListByGovernmentCodeBusinessCodePad(@Param("governmentCode") String governmentCode,
                                                    @Param("businessCode") String businessCode);





    @Select("<script>" +"select  * from  goods where GovernmentCode=#{governmentCode} and GoodsAttributeCodes='' and GoodsType=2 "+
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "</script> ")
    List<GoodsDTO> getByGovernmentCodeAndName(@Param("governmentCode") String governmentCode,@Param("name") String name);


    @Select("select * from goods where Name=#{name} and GovernmentCode=#{governmentCode} and Status=1 limit 1 ")
    Goods getByName(@Param("name") String name,@Param("governmentCode") String governmentCode);

    @Select("select * from goods where GoodsCode=#{goodsCode} limit 1 ")
    GoodsDTO getByGoodsCode(@Param("goodsCode") String goodsCode);


    @Select("<script>" +
            "select * from goods where BusinessCode=#{businessCode} " +
            "<if test ='status!=-1 '>" +
            " and Status=#{status} " +
            "</if>" +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='isRecommend!=-1 '>" +
            " and isRecommend=#{isRecommend} " +
            "</if>" +
            "<if test ='isDelete!=-1 '>" +
            " and status=#{isDelete} " +
            "</if>" +
            "<if test ='isBargainPrice!=-1 '>" +
            " and isBargainPrice=#{isBargainPrice} " +
            "</if>" +
            "<if test ='goodsClassGovernmentCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
            " and GoodsClassGovernmentCode=#{goodsClassGovernmentCode} " +
            "</if>" +
            " order by createTime desc</script> ")
    List<GoodsDTO> getByBusinessCode(@Param("businessCode") String businessCode,
                                     @Param("status") int status,
                                     @Param("name") String name,
                                     @Param("isRecommend") int isRecommend,
                                     @Param("isDelete") int isDelete,
                                     @Param("isBargainPrice") int isBargainPrice,
                                     @Param("goodsClassGovernmentCode") String goodsClassGovernmentCode);


    @Select("select  * from  goods where  GoodsClassGovernmentCode=#{goodsClassGovernmentCode} and GovernmentCode=#{governmentCode} and Status=1  ")
    List<GoodsDTO> getByGoodsClassGovernmentCode(@Param("goodsClassGovernmentCode") String goodsClassGovernmentCode,@Param("governmentCode")String governmentCode);


    @Select("select  * from goods" +
            " where GoodsClassGovernmentCodePath like concat('%',#{goodsClassCode},'%')" +
            " and GovernmentCode=#{governmentCode} and Status=1 and IsDelete=0 ")
    List<GoodsDTO> getFirstGoodsClass(@Param("goodsClassCode") String gooodsClssCode,@Param("governmentCode")String governmentCode);


    @Select("<script>" +
            "select * from goods where GovernmentCode=#{governmentCode} " +
            "and Status=1 " +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "</script> ")
    List<GoodsDTO> getByGoodsName(@Param("name") String name,@Param("governmentCode")String governmentCode);

    @Select("select  * from  goods where GoodsClassBusinessCodePath like concat('%',#{goodsClassBusinessCode},'%') order by PriceMin ")
    List<GoodsDTO> getByGoodsClassBusinessCode(@Param("goodsClassBusinessCode") String goodsClassBusinessCode);

    @Select("<script>" +
            "select * from goods where GovernmentCode=#{governmentCode} " +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='isRecommend!=-1 '>" +
            " and isRecommend=#{isRecommend} " +
            "</if>" +
            "<if test ='isDelete!=-1 '>" +
            " and isDelete=#{isDelete} " +
            "</if>" +
            "<if test ='isBargainPrice!=-1 '>" +
            " and isBargainPrice=#{isBargainPrice} " +
            "</if>" +
            "<if test ='goodsClassGovernmentCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
            " and goodsClassGovernmentCode=#{goodsClassGovernmentCode} " +
            "</if>" +
            "</script> ")
    List<GoodsDTO> getPageByGovernmentCode(@Param("governmentCode") String governmentCode,
                                        @Param("name") String name,
                                        @Param("isRecommend") int isRecommend,
                                        @Param("isDelete") int isDelete,
                                        @Param("isBargainPrice") int isBargainPrice,
                                        @Param("goodsClassGovernmentCode") String goodsClassGovernmentCode);


    @Select("select * from goods where" +
            " businessCode=#{businessCode} " +
            " and Status=1 " +
            " and instr(Name,#{key})!=0 ")
    List<GoodsDTO> getListByBusinessCodeKey(@Param("businessCode") String businessCode, @Param("key")String key);

    @Select("select * from goods ")
    List<Goods> getGoods();






    /**
     * 商家的正常商品列表
     * */
    @Select("<script>" +
            "select GoodsCode from goods" +
            " where BusinessCode=#{businessCode} " +
            "<if test ='status!=-1 '>" +
            " and Status=#{status} " +
            "</if>" +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='goodsClassGovernmentCode != null and goodsClassGovernmentCode !=\"\" '>" +
            " and GoodsClassGovernmentCode=#{goodsClassGovernmentCode} " +
            "</if>" +
            " order by createTime desc</script> ")
    List<Goods> ListByBusinessCodeNormal(@Param("businessCode") String businessCode,
                                         @Param("name") String name,
                                         @Param("status") int status,
                                         @Param("goodsClassGovernmentCode") String goodsClassGovernmentCode);



    /**
     * 商家删除的商品列表
     * */
    @Select("<script>" +
            "select GoodsCode from goods" +
            " where BusinessCode=#{businessCode} and IsDelete = 1 " +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            " order by createTime desc</script> ")
    List<Goods> ListByBusinessCodeDelete(@Param("businessCode") String businessCode,
                                         @Param("name") String name);



    /**
     * 商家收银机的商品列表
     * */
    @Select("<script>" +
            "select GoodsCode from goods" +
            " where BusinessCode=#{businessCode} and isPad = 1 " +
            " order by createTime desc</script> ")
    List<Goods> ListByBusinessCodePad(@Param("businessCode") String businessCode);




}
